import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'myproject.settings')
import django
django.setup()

import json
from django.core import serializers
from app.models import Root,UserInfo
from acticle.models import ActicleType

def generator_json():
    json_data = serializers.serialize('json', Root.objects.all())
    with open("./root_data.json", "w", encoding="utf8") as f:
        f.write(json_data)

def generator_orm():
    with open("./root_data.json", "r", encoding='utf-8') as f:
        line = json.load(f, encoding="gb2312")
        for i in line['RootData']:
            Root.objects.create(name=i["name"],id=i['id'])

        for i in line['UserInfo']:
            UserInfo.objects.create(user_nickname=i["user_nickname"],user_name=i["user_name"],user_psd=i['user_psd'],root_id=i['root_id'])
 
        for i in line['ActicleType']:
            ActicleType.objects.create(id=i['id'],name=i['name'])

if __name__ == '__main__':
    # generator_json()
    generator_orm()
    